set +x
set -eu; set -o pipefail

DockerManagerHost={{.DockerManagerHost}}
DockerManagerUser={{.DockerManagerUser}}
DockerManagerPort={{.DockerManagerPort}}

BackendServiceName={{.BackendServiceName}}
CeleryWorkerServiceName={{.CeleryWorkerServiceName}}
CeleryBeatServiceName={{.CeleryBeatServiceName}}

Action=$Action


case $Action in
   "restart")
      echo "try to restart backend and celery"
      ssh -p $DockerManagerPort $DockerManagerUser@$DockerManagerHost \
         sudo docker service scale $BackendServiceName=0\; \
         sudo docker service scale $BackendServiceName=1\; \
         sudo docker service scale $CeleryBeatServiceName=0\; \
         sudo docker service scale $CeleryBeatServiceName=1\; \
         sudo docker service scale $CeleryWorkerServiceName=0\; \
         sudo docker service scale $CeleryWorkerServiceName=1
   ;;
   "stop")
      echo "try to stop backend and clelery"
      ssh -p $DockerManagerPort $DockerManagerUser@$DockerManagerHost \
         sudo docker service scale $BackendServiceName=0\; \
         sudo docker service scale $CeleryBeatServiceName=0\; \
         sudo docker service scale $CeleryWorkerServiceName=0
   ;;
   *)
      echo "action should be restart or stop"
      exit1
   ;;
esac
